package qldc.pm.tiensu.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import qldc.pm.qtht.dmdc.model.QuanHeGiaDinh;
import qldc.pm.qtht.dmdc.service.QuanHeGiaDinhLocalServiceUtil;
import qldc.pm.qtht.dmdc.utils.DiaChiUtils;
import qldc.pm.tiensu.NoSuchDoiTuongTienSuException;
import qldc.pm.tiensu.NoSuchLichSuXoaAnTichException;
import qldc.pm.tiensu.constants.TienSuConstants;
import qldc.pm.tiensu.constants.TrangThaiTienSuEnum;
import qldc.pm.tiensu.constants.TrangThaiTienSuTrongThongKeEnum;
import qldc.pm.tiensu.dto.DS_HoGiaDinhDTO;
import qldc.pm.tiensu.dto.DS_LichSuPhamToiDTO;
import qldc.pm.tiensu.dto.DS_NguoiTienSuDTO;
import qldc.pm.tiensu.dto.DS_TaiPhamDTO;
import qldc.pm.tiensu.dto.DS_XoaAnTichDTO;
import qldc.pm.tiensu.dto.HoGiaDinhDTO;
import qldc.pm.tiensu.dto.SoHoKhauDTO;
import qldc.pm.tiensu.model.DoiTuongTienSu;
import qldc.pm.tiensu.model.TienSu;
import qldc.pm.tiensu.service.DoiTuongTienSuLocalServiceUtil;
import qldc.pm.tiensu.service.LichSuXoaAnTichLocalServiceUtil;
import qldc.pm.tiensu.service.TienSuLocalServiceUtil;
import qldc.pm.tiensu.service.base.DoiTuongTienSuLocalServiceBaseImpl;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.User;

/**
 * The implementation of the doi tuong tien su local service.
 *
 * <p>
 * All custom service methods should be put in this class. Whenever methods are
 * added, rerun ServiceBuilder to copy their definitions into the
 * {@link qldc.pm.tiensu.service.DoiTuongTienSuLocalService} interface.
 *
 * <p>
 * This is a local service. Methods of this service will not have security
 * checks based on the propagated JAAS credentials because this service can only
 * be accessed from within the same VM.
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see qldc.pm.tiensu.service.base.DoiTuongTienSuLocalServiceBaseImpl
 * @see qldc.pm.tiensu.service.DoiTuongTienSuLocalServiceUtil
 */
public class DoiTuongTienSuLocalServiceImpl extends
		DoiTuongTienSuLocalServiceBaseImpl {
	private static Log _log = LogFactoryUtil
			.getLog(DoiTuongTienSuLocalServiceImpl.class);
	private SimpleDateFormat sdf = new SimpleDateFormat(TienSuConstants.DATE_PATTERN);

	public DoiTuongTienSu findByDoiTuongTienSuId(long doiTuongTienSuId) {
		DoiTuongTienSu doiTuongTienSu = null;

		try {
			doiTuongTienSu = doiTuongTienSuPersistence
					.findByPrimaryKey(doiTuongTienSuId);
		} catch (NoSuchDoiTuongTienSuException e) {
			_log.warn(e.getMessage());
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

		return doiTuongTienSu;
	}
	public TienSu layTienSuDangThiHanhAnCuaDoiTuong(String soDinhDanhCaNhan){
		try {
			DoiTuongTienSu doiTuong = findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

			if (Validator.isNotNull(doiTuong)){
				List<TienSu> list = tienSuPersistence.findByDoiTuongTienSuId(doiTuong.getDoiTuongTienSuId());
				for (TienSu ts : list){
					TrangThaiTienSuEnum tr = TienSuLocalServiceUtil.kiemTraTrangThaiCuaTienSu(ts.getTienSuId());
					if (tr.getValue() == TrangThaiTienSuEnum.DANG_THI_HANH_AN.getValue() || tr.getValue() == TrangThaiTienSuEnum.DANG_LAP_YEU_CAU_XOA_AN_TICH.getValue()  ) {
						return ts;
					}
				}
			}
		}
		 catch (NoSuchLichSuXoaAnTichException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
		} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
		}
		return null;
	}
	public List<TienSu> findTienSuBySoSoHoKhau(String soSoHoKhau) throws SystemException{
		return doiTuongTienSuFinder.findTienSuBySoSoHoKhau(soSoHoKhau);
	}
	public List<TienSu> layDanhSachTienSuDangThiHanhAnCuaDoiTuong(String soDinhDanhCaNhan){
		List<TienSu> tienSus = new ArrayList<TienSu>();
		try {
			DoiTuongTienSu doiTuong = findBySoDinhDanhCaNhan(soDinhDanhCaNhan);

			if (Validator.isNotNull(doiTuong)){
				List<TienSu> list = tienSuPersistence.findByDoiTuongTienSuId(doiTuong.getDoiTuongTienSuId());
				for (TienSu ts : list){
					TrangThaiTienSuEnum tr = TienSuLocalServiceUtil.kiemTraTrangThaiCuaTienSu(ts.getTienSuId());
					if (tr.getValue() == TrangThaiTienSuEnum.DANG_THI_HANH_AN.getValue() || tr.getValue() == TrangThaiTienSuEnum.DANG_LAP_YEU_CAU_XOA_AN_TICH.getValue()  ) {
						tienSus.add(ts);
					}
				}
			}
		}
		 catch (NoSuchLichSuXoaAnTichException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return new ArrayList<TienSu>();
		} catch (SystemException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return new ArrayList<TienSu>();
		}
		return tienSus;
	}

	public DoiTuongTienSu findBySoDinhDanhCaNhan(String soDinhDanhCaNhan) {
		DoiTuongTienSu doiTuongTienSu = null;

		try {
			doiTuongTienSu = doiTuongTienSuPersistence
					.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);
		} catch (NoSuchDoiTuongTienSuException e) {
			_log.warn(e.getMessage());
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

		return doiTuongTienSu;
	}

	public DoiTuongTienSu addDoiTuongTienSu(User user, String soDinhDanhCaNhan)
			throws SystemException {
		long doiTuongTienSuId = counterLocalService.increment(DoiTuongTienSu.class.getName());
		DoiTuongTienSu doiTuongTienSu = createDoiTuongTienSu(doiTuongTienSuId);

		doiTuongTienSu.setUserId(user.getUserId());
		doiTuongTienSu.setUserName(user.getScreenName());
		doiTuongTienSu.setCreateDate(new Date());

		doiTuongTienSu.setSoDinhDanhCaNhan(soDinhDanhCaNhan);

		doiTuongTienSu = addDoiTuongTienSu(doiTuongTienSu);

		return doiTuongTienSu;
	}


	public int countBySDDCN_CMND_HVT_SSHK_DCTT(String soDinhDanhCaNhan,
			String soCMND, String hoVaTen, String soSoHoKhau, long tinhId,
			long quanHuyenId, long phuongXaId, String diaChi, Date tuNgayPhamToi,
			Date denNgayPhamToi,String banAnSo, Date tuNgayToaAn, Date denNgayToaAn, int trangThai) {
		int count = 0;
		try {
			count = doiTuongTienSuFinder.countBySDDCN_CMND_HVT_SSHK_DCTT(soDinhDanhCaNhan, soCMND, hoVaTen, soSoHoKhau, tinhId, quanHuyenId, phuongXaId, diaChi, tuNgayPhamToi, denNgayPhamToi, banAnSo, tuNgayToaAn, denNgayToaAn, trangThai);
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

		return count;
	}

	public List<Object[]> findBySDDCN_CMND_HVT_SSHK_DCTT(
			String soDinhDanhCaNhan, String soCMND, String hoVaTen,
			String soSoHoKhau, long tinhId, long quanHuyenId, long phuongXaId,
			String diaChi, Date tuNgayPhamToi,
			Date denNgayPhamToi, String banAnSo, Date tuNgayToaAn, Date denNgayToaAn, int trangThai, int start, int end, OrderByComparator obc) {
		try {
			 return doiTuongTienSuFinder
					.findBySDDCN_CMND_HVT_SSHK_DCTT(soDinhDanhCaNhan, soCMND, hoVaTen, soSoHoKhau, tinhId, quanHuyenId, phuongXaId, diaChi, tuNgayPhamToi, denNgayPhamToi, banAnSo, tuNgayToaAn, denNgayToaAn, trangThai, start, end, obc);
		} catch (SystemException e) {
			_log.warn(e.getMessage());
		}

		return null;
	}

	public int countBySSHK_DCTT(String soSoHoKhau, Date tuNgayPhamToi,
			Date denNgayPhamToi, long tinhId, long quanHuyenId,
			long phuongXaId, String diaChi) throws SystemException {
		return doiTuongTienSuFinder.countBySSHK_DCTT(soSoHoKhau, tuNgayPhamToi,
				denNgayPhamToi, tinhId, quanHuyenId, phuongXaId, diaChi);
	}
	public List<HoGiaDinhDTO> findDsHoGiaDinhByVungDiaLy_ThoiGianToaAn(long tinhId, long quanHuyenId, long phuongXaId, Date tuNgayToaAn, Date denNgayToaAn, int start, int end, OrderByComparator order) throws SystemException{
		List<Object[]> list = doiTuongTienSuFinder.findDSHoGiaDinhByVungDiaLy_ThoiGianToaAn(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn,start,end,order);
		List<HoGiaDinhDTO> result = new ArrayList<HoGiaDinhDTO>();
		for (int i = 0; i < list.size(); i++) {
			Object[] objDoiTuong = list.get(i);
			HoGiaDinhDTO hoGiaDinh = new HoGiaDinhDTO();
			long diaChiThuongTruTinhId = Long.parseLong(objDoiTuong[0].toString());
			long diaChiThuongTruQuanHuyenId = Long.parseLong(objDoiTuong[1].toString());
			long diaChiThuongTruPhuongXaId = Long.parseLong(objDoiTuong[2].toString());
			String diaChiThuongTru = objDoiTuong[3].toString();
			String diaChi = DiaChiUtils.createBlockDiaChi(diaChiThuongTruTinhId, diaChiThuongTruQuanHuyenId, diaChiThuongTruPhuongXaId, diaChiThuongTru);
			hoGiaDinh.setDiaChiThuongTru(diaChi);
			hoGiaDinh.setSoSoHoKhau(objDoiTuong[4].toString());
			hoGiaDinh.setHoVaTenChuHo(objDoiTuong[5].toString());
			hoGiaDinh.setSoLuongNguoiPhamToi(Integer.parseInt(objDoiTuong[6].toString()));
			result.add(hoGiaDinh);
		}
		return result;
	}
	public int countDsHoGiaDinhByVungDiaLy_ThoiGianToaAn(long tinhId, long quanHuyenId, long phuongXaId, Date tuNgayToaAn, Date denNgayToaAn) throws SystemException{
		return doiTuongTienSuFinder.countDSHoGiaDinhByVungDiaLy_ThoiGianToaAn(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn);
	}
	public int countDsTienSuByVungDiaLy_ThoiGianToaAn(long tinhId,
			long quanHuyenId, long phuongXaId,
			Date tuNgayToaAn, Date denNgayToaAn, int xoaAnTich) throws SystemException{
		return doiTuongTienSuFinder.countDsTienSuByVungDiaLy_ThoiGian(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn, xoaAnTich);
	}

	public List<DS_NguoiTienSuDTO> findDsTienSuByVungDiaLy_ThoiGianToaAn(long tinhId,
			long quanHuyenId, long phuongXaId,
			Date tuNgayToaAn, Date denNgayToaAn, int xoaAnTich, int start, int end, OrderByComparator order)
			throws SystemException {
		try {
			List<DS_NguoiTienSuDTO> DanhSachTienSus = new ArrayList<DS_NguoiTienSuDTO>();

			List<Object[]> objects = doiTuongTienSuFinder.findDsTienSuByVungDiaLy_ThoiGian(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn, xoaAnTich,start,end,order);

			int length = (objects != null ? objects.size() : 0);

			if (length > 0) {
				Object[] objDoiTuong = null;

				DS_NguoiTienSuDTO doiTuongTienSu = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					doiTuongTienSu = new DS_NguoiTienSuDTO();
					doiTuongTienSu.setHoTen(objDoiTuong[4] != null ? objDoiTuong[4].toString() : "");
					doiTuongTienSu.setSoDinhDanhCaNhan(objDoiTuong[5] != null ? objDoiTuong[5].toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(Long.parseLong(objDoiTuong[0].toString()),Long.parseLong(objDoiTuong[1].toString()),Long.parseLong(objDoiTuong[2].toString()),objDoiTuong[3].toString());
					doiTuongTienSu.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru: "");
					doiTuongTienSu.setSoCMND(objDoiTuong[6] != null ? objDoiTuong[6].toString() : "");
					int soLanPhamToi = objDoiTuong[9] != null ? Integer.valueOf(objDoiTuong[9].toString()) : 0;
					String toiDanh = objDoiTuong[7] != null ? objDoiTuong[7].toString() : "";

					doiTuongTienSu.setToiDanh(toiDanh);

					if (objDoiTuong[10] != null) {
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[10].toString());
						doiTuongTienSu.setNgaySinh(ngaySinh);
					}

					int trangThai = TrangThaiTienSuTrongThongKeEnum.DANG_THI_HANH_AN.getValue();

					TienSu tienSuMoiNhatDangThiHanhAn = layTienSuDangThiHanhAnCuaDoiTuong(doiTuongTienSu.getSoDinhDanhCaNhan());

					boolean daXoaAnTich = Validator.isNull(tienSuMoiNhatDangThiHanhAn);

					if (daXoaAnTich) {
						trangThai = TrangThaiTienSuTrongThongKeEnum.DA_XOA_AN_TICH.getValue();
					} else {
						doiTuongTienSu.setNgayToaAnMoiNhat(tienSuMoiNhatDangThiHanhAn.getNgayToaAn());
					}

					doiTuongTienSu.setTrangThai(trangThai);
					doiTuongTienSu.setSoLanPhamToi(soLanPhamToi);

					if (xoaAnTich == TrangThaiTienSuTrongThongKeEnum.TAT_CA.getValue()) {
						DanhSachTienSus.add(doiTuongTienSu);
					} else if (trangThai == TrangThaiTienSuTrongThongKeEnum.DA_XOA_AN_TICH.getValue() && xoaAnTich == TrangThaiTienSuTrongThongKeEnum.DA_XOA_AN_TICH.getValue()){
						DanhSachTienSus.add(doiTuongTienSu);
					} else if (trangThai == TrangThaiTienSuTrongThongKeEnum.DANG_THI_HANH_AN.getValue() && xoaAnTich == TrangThaiTienSuTrongThongKeEnum.DANG_THI_HANH_AN.getValue()){
						DanhSachTienSus.add(doiTuongTienSu);
					}
				}

				return DanhSachTienSus;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<DS_NguoiTienSuDTO>();
		}

		return  new ArrayList<DS_NguoiTienSuDTO>();
	}

	private boolean isContain(String[] list, String item){
		if (Validator.isNull(list)) {
			return false;
		}
		for (String str : list){
			System.out.println("str > "+str+" item "+item);
			if (str.equals(item)) {
				return true;
			}
		}
		return false;
	}

	public List<DS_NguoiTienSuDTO> createDataForReportDanhSachDoiTuongCanXoaAnTich(long tinhId,
			long quanHuyenId, long phuongXaId,
			Date tuNgayToaAn, Date denNgayToaAn, String[] soDinhDanhCaNhans)
			{
		try {

			List<Object[]> objects = doiTuongTienSuFinder.findDsTienSuByVungDiaLy_ThoiGian(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn, TrangThaiTienSuEnum.DANG_THI_HANH_AN.getValue(),-1,-1,null);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
				List<DS_NguoiTienSuDTO> DanhSachTienSus = new ArrayList<DS_NguoiTienSuDTO>();

				for (int i = 0; i < length; i++) {
					Object[] objDoiTuong  = objects.get(i);
					String soDinhDanhCaNhan = objDoiTuong[5] != null ? objDoiTuong[5].toString() : "";
					DS_NguoiTienSuDTO nguoiTienSuDTO = new DS_NguoiTienSuDTO();

					if (isContain(soDinhDanhCaNhans, soDinhDanhCaNhan)){
					nguoiTienSuDTO.setHoTen(objDoiTuong[4] != null ? objDoiTuong[4].toString() : "");
					nguoiTienSuDTO.setSoDinhDanhCaNhan(soDinhDanhCaNhan);
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(Long.parseLong(objDoiTuong[0].toString()),Long.parseLong(objDoiTuong[1].toString()),Long.parseLong(objDoiTuong[2].toString()),objDoiTuong[3].toString());
					nguoiTienSuDTO.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru: "");
					nguoiTienSuDTO.setSoCMND(objDoiTuong[6] != null ? objDoiTuong[6].toString() : "");
					int soLanPhamToi = objDoiTuong[9] != null ? Integer.valueOf(objDoiTuong[9].toString()) : 0;
					String toiDanh = objDoiTuong[7] != null ? objDoiTuong[7].toString() : "";

					nguoiTienSuDTO.setToiDanh(toiDanh);

					if (objDoiTuong[10] != null){
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[10].toString());
						nguoiTienSuDTO.setNgaySinh(ngaySinh);
					}
					List<TienSu> tienSuDangThiHanhAns = layDanhSachTienSuDangThiHanhAnCuaDoiTuong(nguoiTienSuDTO.getSoDinhDanhCaNhan());

					nguoiTienSuDTO.setTrangThai(TrangThaiTienSuTrongThongKeEnum.DANG_THI_HANH_AN.getValue());
					nguoiTienSuDTO.setSoLanPhamToi(soLanPhamToi);
						for (TienSu ts : tienSuDangThiHanhAns){
							DS_NguoiTienSuDTO d1 = nguoiTienSuDTO.clone(nguoiTienSuDTO);
							d1.setNgayToaAn(ts.getNgayToaAn());
							d1.setToiDanh(ts.getToiDanh());
							d1.setToaAnPhat(ts.getToaAnXuPhat());
							d1.setHinhPhat(ts.getHinhPhatChinh());
							d1.setNewDoiTuong(false);

							if (!isContain(DanhSachTienSus, d1)){
								int stt = i+1;
								d1.setStt(stt);
								d1.setNewDoiTuong(true);
							}
							DanhSachTienSus.add(d1);
						}
					}
				}

				return DanhSachTienSus;
		} catch (Exception e) {
			System.out.println("E > "+e.getMessage());
			return null;
		}

	}
	public List<DS_NguoiTienSuDTO> createDataForReportDanhSachDoiTuongCanXoaAnTichExpandDiv(long tinhId,
			long quanHuyenId, long phuongXaId,
			Date tuNgayToaAn, Date denNgayToaAn, String[] soDinhDanhCaNhans, String[] tienSuIds)
			{
		try {

			List<Object[]> objects = doiTuongTienSuFinder.findDsTienSuByVungDiaLy_ThoiGian(tinhId, quanHuyenId, phuongXaId, tuNgayToaAn, denNgayToaAn, TrangThaiTienSuEnum.DANG_THI_HANH_AN.getValue(),-1,-1,null);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
				List<DS_NguoiTienSuDTO> DanhSachTienSus = new ArrayList<DS_NguoiTienSuDTO>();

				for (int i = 0; i < length; i++) {
					Object[] objDoiTuong  = objects.get(i);
					String soDinhDanhCaNhan = objDoiTuong[5] != null ? objDoiTuong[5].toString() : "";
					DS_NguoiTienSuDTO nguoiTienSuDTO = new DS_NguoiTienSuDTO();

					if (isContain(soDinhDanhCaNhans, soDinhDanhCaNhan)){
					nguoiTienSuDTO.setHoTen(objDoiTuong[4] != null ? objDoiTuong[4].toString() : "");
					nguoiTienSuDTO.setSoDinhDanhCaNhan(soDinhDanhCaNhan);
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(Long.parseLong(objDoiTuong[0].toString()),Long.parseLong(objDoiTuong[1].toString()),Long.parseLong(objDoiTuong[2].toString()),objDoiTuong[3].toString());
					nguoiTienSuDTO.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru: "");
					nguoiTienSuDTO.setSoCMND(objDoiTuong[6] != null ? objDoiTuong[6].toString() : "");
					int soLanPhamToi = objDoiTuong[9] != null ? Integer.valueOf(objDoiTuong[9].toString()) : 0;
					String toiDanh = objDoiTuong[7] != null ? objDoiTuong[7].toString() : "";

					nguoiTienSuDTO.setToiDanh(toiDanh);

					if (objDoiTuong[10] != null){
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[10].toString());
						nguoiTienSuDTO.setNgaySinh(ngaySinh);
					}

					nguoiTienSuDTO.setTrangThai(TrangThaiTienSuTrongThongKeEnum.DANG_THI_HANH_AN.getValue());
					nguoiTienSuDTO.setSoLanPhamToi(soLanPhamToi);
						for (String tienSuId: tienSuIds){
							System.out.println("tienSuId >>>>>>>>>>>>>>>>>>>"+tienSuId);
							TienSu ts = tienSuLocalService.findByTienSuId(Long.parseLong(tienSuId));
							if(Validator.isNotNull(ts)){
								DoiTuongTienSu doiTuong = doiTuongTienSuLocalService.findByDoiTuongTienSuId(ts.getDoiTuongTienSuId());
								if(Validator.isNotNull(doiTuong)){
									if (doiTuong.getSoDinhDanhCaNhan().equals(soDinhDanhCaNhan)){
										DS_NguoiTienSuDTO d1 = nguoiTienSuDTO.clone(nguoiTienSuDTO);
										d1.setNgayToaAn(ts.getNgayToaAn());
										d1.setToiDanh(ts.getToiDanh());
										d1.setToaAnPhat(ts.getToaAnXuPhat());
										d1.setHinhPhat(ts.getHinhPhatChinh());
										d1.setNewDoiTuong(false);
										System.out.println("d1 >>>>>>>>>>>>>>>>>>>>>,"+d1.getSoDinhDanhCaNhan());
										if (!isContain(DanhSachTienSus, d1)){
											int stt = i+1;
											d1.setStt(stt);
											d1.setNewDoiTuong(true);
										}
										DanhSachTienSus.add(d1);
									}
								}
							}
						}
					}

				}

				return DanhSachTienSus;
		} catch (Exception e) {
			System.out.println("E > "+e.getMessage());
			return null;
		}

	}

	private boolean isContain(List<DS_NguoiTienSuDTO> danhSachTienSus,
			DS_NguoiTienSuDTO nguoiTienSuDTO) {
		for (DS_NguoiTienSuDTO obj : danhSachTienSus){
			if (obj.getSoDinhDanhCaNhan().equals(nguoiTienSuDTO.getSoDinhDanhCaNhan())) {
				return true;
			}
		}
		return false;
	}
	public int countByDS_XoaAnTich(long tinhId,
			long quanHuyenId, long phuongXaId, Date tuNgayXoaAnTich, Date denNgayXoaAnTich)
			throws SystemException {
		return doiTuongTienSuFinder.countByDS_XoaAnTich(tinhId, quanHuyenId, phuongXaId, tuNgayXoaAnTich, denNgayXoaAnTich);

	}
	public List<DS_XoaAnTichDTO> findByDS_XoaAnTich(long tinhId,
			long quanHuyenId, long phuongXaId, Date tuNgayXoaAnTich, Date denNgayXoaAnTich,  int start, int end, OrderByComparator order)
			throws SystemException {

		try {
			sdf = new SimpleDateFormat("dd/MM/yyyy");
			List<Object[]> objects = doiTuongTienSuFinder.findByDS_XoaAnTich(
					 tinhId,
					quanHuyenId, phuongXaId, tuNgayXoaAnTich, denNgayXoaAnTich,start,end,order);

			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DS_XoaAnTichDTO> danhSachXoaAnTichs = new ArrayList<DS_XoaAnTichDTO>();

				Object[] objDoiTuong = null;
				DS_XoaAnTichDTO danhSachXoaAnTich = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					danhSachXoaAnTich = new DS_XoaAnTichDTO();
					danhSachXoaAnTich
							.setSoSoHoKhau(objDoiTuong[0] != null ? objDoiTuong[0]
									.toString() : "");
					danhSachXoaAnTich
							.setHoTen(objDoiTuong[5] != null ? objDoiTuong[5]
									.toString() : "");
					danhSachXoaAnTich
							.setSoDinhDanhCaNhan(objDoiTuong[6] != null ? objDoiTuong[6]
									.toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[1].toString()),
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							objDoiTuong[4].toString());
					danhSachXoaAnTich
							.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru
									: "");
					danhSachXoaAnTich
							.setSoCMND(objDoiTuong[7] != null ? objDoiTuong[7]
									.toString() : "");

					int soLanPhamToi = objDoiTuong[10] != null ? Integer
							.valueOf(objDoiTuong[10].toString()) : 0;
					String toiDanh = objDoiTuong[8] != null ? objDoiTuong[8].toString() : "";
					if(soLanPhamToi <= 1) {
						toiDanh = toiDanh.substring(0, toiDanh.indexOf(","));
					}

					danhSachXoaAnTich.setToiDanh(toiDanh);

					String ngayPhamToi = objDoiTuong[9] != null ? objDoiTuong[9].toString() : "";
					if(soLanPhamToi <= 1) {
						ngayPhamToi = ngayPhamToi.substring(0, 9);
					}

					danhSachXoaAnTich.setNgayPhamToi(ngayPhamToi);

					if (objDoiTuong[11] != null){
						Date ngaySinh = new SimpleDateFormat("yyyy-MM-dd").parse(objDoiTuong[11].toString());
						danhSachXoaAnTich.setNgaySinh(ngaySinh);
					}

					danhSachXoaAnTich
							.setSoLanPhamToi(soLanPhamToi);
					if (objDoiTuong[12] != null){
						danhSachXoaAnTich.setGioiTinh(Integer.parseInt(objDoiTuong[12].toString()));
					}

					/*String soDinhDanhCaNhan = objDoiTuong[6] != null ? objDoiTuong[6]
							.toString() : "";
					DoiTuongTienSu doiTuongTienSu = DoiTuongTienSuLocalServiceUtil.findBySoDinhDanhCaNhan(soDinhDanhCaNhan);
					StringBuffer ngayXoaAnTich = new StringBuffer();		 
					List<Date> ngayXoaAnTichs = LichSuXoaAnTichLocalServiceUtil.findByNgayKyDuyetXoaAnTich(tuNgayXoaAnTich,
							denNgayXoaAnTich,doiTuongTienSu.getDoiTuongTienSuId());
					if(ngayXoaAnTichs.size()<2) {
						ngayXoaAnTich.append(sdf.format(ngayXoaAnTichs.get(0)));
					} else {
						for( Date s : ngayXoaAnTichs) {
							ngayXoaAnTich.append(sdf.format(s) + ",");
						}
					}

					
						danhSachXoaAnTich.setNgayXoaAnTich(ngayXoaAnTich.toString());*/
					

					danhSachXoaAnTichs.add(danhSachXoaAnTich);

				}

				return danhSachXoaAnTichs;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	public int countByLichSuPhamToi(String soDinhDanhCaNhan, String hoVaTen,
			long tinhId, long quanHuyenId, long phuongXaId, String diaChi)
			throws SystemException {
		return doiTuongTienSuFinder.countByLichSuPhamToi(soDinhDanhCaNhan,
				hoVaTen, tinhId, quanHuyenId, phuongXaId, diaChi);
	}

	public List<DS_LichSuPhamToiDTO> findByLichSuPhamToi(
			String soDinhDanhCaNhan, String hoVaTen, long tinhId,
			long quanHuyenId, long phuongXaId, String diaChi, int start, int end)
			throws SystemException {

		try {
			List<Object[]> objects = doiTuongTienSuFinder.findByLichSuPhamToi(
					soDinhDanhCaNhan, hoVaTen, tinhId, quanHuyenId, phuongXaId,
					diaChi, start, end);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DS_LichSuPhamToiDTO> DanhSachXoaAnTichs = new ArrayList<DS_LichSuPhamToiDTO>();

				Object[] objDoiTuong = null;
				DS_LichSuPhamToiDTO danhSachLichSuPhamToi = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					danhSachLichSuPhamToi = new DS_LichSuPhamToiDTO();
					danhSachLichSuPhamToi
							.setDoiTuongTienSuId(objDoiTuong[0] != null ? Long
									.valueOf(objDoiTuong[0].toString()) : 0L);
					danhSachLichSuPhamToi
							.setHoVaTen(objDoiTuong[5] != null ? objDoiTuong[5]
									.toString() : "");
					danhSachLichSuPhamToi
							.setSoDinhDanhCaNhan(objDoiTuong[6] != null ? objDoiTuong[6]
									.toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[1].toString()),
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							objDoiTuong[4].toString());
					danhSachLichSuPhamToi
							.setDiaChi(diaChiThuongTru != null ? diaChiThuongTru
									: "");

					danhSachLichSuPhamToi
							.setSoLanPhamToi(objDoiTuong[7] != null ? Integer
									.valueOf(objDoiTuong[7].toString()) : 0);

					DanhSachXoaAnTichs.add(danhSachLichSuPhamToi);

				}

				return DanhSachXoaAnTichs;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	public int countBySoSoHoKhau(long tinhId, long quanHuyenId,
			long phuongXaId, String diaChi, Date tuNgayPhamToi,
			Date denNgayPhamToi) throws SystemException {
		return doiTuongTienSuFinder.countBySoHoKhau(tinhId, quanHuyenId,
				phuongXaId, diaChi, tuNgayPhamToi, denNgayPhamToi);
	}

	public List<SoHoKhauDTO> findBySoSoHoKhau(long tinhId,
			long quanHuyenId, long phuongXaId, String diaChi,
			Date tuNgayPhamToi, Date denNgayPhamToi, int start, int end)
			throws SystemException {

		try {
			List<Object[]> objects = doiTuongTienSuFinder.findBySoHoKhau(tinhId, quanHuyenId, phuongXaId,
					diaChi, tuNgayPhamToi, denNgayPhamToi, start, end);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<SoHoKhauDTO> dsSoHoKhau = new ArrayList<SoHoKhauDTO>();

				Object[] objDoiTuong = null;
				SoHoKhauDTO soHoKhau = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					soHoKhau = new SoHoKhauDTO();
					soHoKhau.setSoSoHokhau(objDoiTuong[0] != null ? objDoiTuong[0].toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[1].toString()),
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							objDoiTuong[4].toString());
					soHoKhau.setDiaChi(diaChiThuongTru);

					dsSoHoKhau.add(soHoKhau);

				}

				return dsSoHoKhau;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}

	public List<DS_HoGiaDinhDTO> findByDS_HoGiaDinh(String soSoHoKhau)
			throws SystemException {

		try {
			List<Object[]> objects = doiTuongTienSuFinder.findByDS_HoGiaDinh(soSoHoKhau);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DS_HoGiaDinhDTO> dsNguoiCoTienSu = new ArrayList<DS_HoGiaDinhDTO>();

				Object[] objDoiTuong = null;
				DS_HoGiaDinhDTO nguoiCoTienSu = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					nguoiCoTienSu = new DS_HoGiaDinhDTO();

					nguoiCoTienSu
							.setHoVaTen(objDoiTuong[0] != null ? objDoiTuong[0].toString() : "");
					nguoiCoTienSu
					.setNgaySinh((Date) (objDoiTuong[1] != null ? objDoiTuong[1] : null));

					String gioiTinh = "";
					if (objDoiTuong[2] != null) {
						gioiTinh = objDoiTuong[2].toString().equalsIgnoreCase("0") ? "Nam" : "Nu";
					}

					nguoiCoTienSu
					.setGioiTinh(gioiTinh);
					nguoiCoTienSu
					.setSoCMND(objDoiTuong[3] != null ? objDoiTuong[3].toString() : "");
					String quanHe = "";
					if (objDoiTuong[4] != null) {

					QuanHeGiaDinh quanHeGiaDinh = QuanHeGiaDinhLocalServiceUtil.findById(Long.valueOf(objDoiTuong[4].toString()));
					quanHe = quanHeGiaDinh.getTenQuanHeGiaDinh();
					}

					nguoiCoTienSu
					.setQuanHeVoiChuHo(quanHe);


					int soLanPhamToi = objDoiTuong[9] != null ? Integer
							.valueOf(objDoiTuong[9].toString()) : 0;

					String toiDanh = objDoiTuong[6] != null ? objDoiTuong[6].toString() : "";
					toiDanh = toiDanh.substring(0, toiDanh.lastIndexOf(","));
					if(soLanPhamToi > 1) {
						toiDanh = toiDanh.substring(0, toiDanh.lastIndexOf(","));
					}
					nguoiCoTienSu.setToiDanh(toiDanh);

					String ngayPhamToi = (objDoiTuong[5] != null ? objDoiTuong[5].toString() : "");
					ngayPhamToi = ngayPhamToi.substring(0, ngayPhamToi.lastIndexOf(","));
					/*if(soLanPhamToi > 1) {
						ngayPhamToi = ngayPhamToi.substring(0, ngayPhamToi.lastIndexOf(","));
					}*/
					nguoiCoTienSu
					.setNgayPhamToi(ngayPhamToi);

					String hinhPhat = (objDoiTuong[7] != null ? objDoiTuong[7].toString() : "");
					hinhPhat = hinhPhat.substring(0, hinhPhat.lastIndexOf(","));
					if(soLanPhamToi > 1) {
						hinhPhat = hinhPhat.substring(0, hinhPhat.lastIndexOf(","));
					}
					nguoiCoTienSu
					.setHinhPhat(objDoiTuong[7] != null ? objDoiTuong[7].toString() : "");

					String ghiChu = (objDoiTuong[8] != null ? objDoiTuong[8].toString() : "");
					ghiChu = ghiChu.substring(0, ghiChu.lastIndexOf(","));
					if(soLanPhamToi > 1) {
						ghiChu = ghiChu.substring(0, ghiChu.lastIndexOf(","));
					}


					nguoiCoTienSu
					.setGhiChu(ghiChu);

					dsNguoiCoTienSu.add(nguoiCoTienSu);

				}

				return dsNguoiCoTienSu;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}


	public int countByDS_TaiPham(long tinhId, long quanHuyenId,
			long phuongXaId, String diaChi, Date tuNgayPhamToi,
			Date denNgayPhamToi) throws SystemException {
		return doiTuongTienSuFinder.countByDS_TaiPham(tuNgayPhamToi, denNgayPhamToi, tinhId, quanHuyenId,
				phuongXaId, diaChi);
	}
	public void xoaAnTichDoiTuongTienSu(long doiTuongTienSuId) throws SystemException{
		List<TienSu> tienSu = TienSuLocalServiceUtil.findByDoiTuongTienSuId(doiTuongTienSuId);
		if (tienSu.size() == 0){
			DoiTuongTienSu dtts = findByDoiTuongTienSuId(doiTuongTienSuId);
			if (Validator.isNotNull(dtts)){
				dtts.setXoaAnTich(true);
				updateDoiTuongTienSu(dtts);
			}
		}
	}

	public List<DS_TaiPhamDTO> findByDS_TaiPham(long tinhId,
			long quanHuyenId, long phuongXaId, String diaChi,
			Date tuNgayPhamToi, Date denNgayPhamToi, int start, int end)
			throws SystemException {
		try {
			List<Object[]> objects = doiTuongTienSuFinder.findByDS_TaiPham(tuNgayPhamToi, denNgayPhamToi, tinhId, quanHuyenId, phuongXaId,
					diaChi, start, end);
			int length = (objects != null ? objects.size() : 0);
			// if found more than 1 doi tuong
			if (length > 0) {
				List<DS_TaiPhamDTO> dsTaiPham = new ArrayList<DS_TaiPhamDTO>();

				Object[] objDoiTuong = null;
				DS_TaiPhamDTO taiPham = null;

				for (int i = 0; i < length; i++) {
					objDoiTuong = objects.get(i);
					taiPham = new DS_TaiPhamDTO();
					taiPham.setSoSoHoKhau(objDoiTuong[0] != null ? objDoiTuong[0].toString() : "");
					String diaChiThuongTru = DiaChiUtils.createBlockDiaChi(
							Long.parseLong(objDoiTuong[1].toString()),
							Long.parseLong(objDoiTuong[2].toString()),
							Long.parseLong(objDoiTuong[3].toString()),
							objDoiTuong[4].toString());
					taiPham.setDiaChi(diaChiThuongTru);
					taiPham.setHoVaTen(objDoiTuong[5] != null ? objDoiTuong[5].toString() : "");
					taiPham.setSoDinhDanhCaNhan(objDoiTuong[6] != null ? objDoiTuong[6].toString() : "");
					taiPham.setSoCMND(objDoiTuong[7] != null ? objDoiTuong[7].toString() : "");
					taiPham.setNgayPhamToi(objDoiTuong[8] != null ? objDoiTuong[8].toString() : "");
					taiPham.setToiDanh(objDoiTuong[9] != null ? objDoiTuong[9].toString() : "");
					taiPham.setToiDanhTruoc(objDoiTuong[10] != null ? objDoiTuong[10].toString() : "");
					taiPham.setNgayPhamToiTruoc(objDoiTuong[11] != null ? objDoiTuong[11].toString() : "");
					taiPham.setNgayXoaAnTich(objDoiTuong[12] != null ? objDoiTuong[12].toString() : "");
					taiPham.setNhanXet(objDoiTuong[13] != null ? objDoiTuong[13].toString() : "");

					dsTaiPham.add(taiPham);

				}

				return dsTaiPham;
			}
		} catch (Exception e) {
			return null;
		}
		return null;
	}
	
	public Date findLatestNgayToaAn_bySoHoKhau(String soHoKhau)
			throws SystemException {
		try{
		return doiTuongTienSuFinder.findLatestNgayToaAn_bySoHoKhau(soHoKhau);
		} catch (Exception e) {
			 return null;
			 
		}
	}
}
